package websocketgulu.demo.service.Impl;

import com.alibaba.fastjson.JSON;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import websocketgulu.demo.Utils.*;
import websocketgulu.demo.dao.Mp3DataDao;
import websocketgulu.demo.pojo.Mp3Data;
import websocketgulu.demo.pojo.Mp3Details;
import websocketgulu.demo.pojo.RecordDetails;
import websocketgulu.demo.service.Mp3DataService;
import websocketgulu.demo.websocket.showDiagramWebSocket;

import javax.servlet.http.HttpServletResponse;

@Service
public class Mp3DataServiceImpl implements Mp3DataService {
    @Autowired
    private Mp3DataDao mp3DataDao;

    @Autowired
    private SendMp3Utils sendMp3Utils;



    public void ParseAndSend(String collection_time, String end_time, String filename)  {

        String target = TestPathUtils.getPath() + filename;
        long userId = 1L;
        this.mp3DataDao.insertMusic(Long.valueOf(userId), collection_time, collection_time, end_time, target);
    }


    /**
     *
     * @param userId 用户的ID
     * @param collection_time 回放请求的起始时间
     * @param end_time
     * @return 返回的是mp3的orm对象
     */
    public Mp3Details loadMp3Details(Long userId, String collection_time, String end_time) {
        Mp3Details mp3 = this.mp3DataDao.loadMp3Details(userId, collection_time, end_time);
        return mp3;
    }


    /**
     * 发送回放信息
     * @param mp3
     * @param start_time
     * @param end_time
     * @return
     */
    public String send_playback(Mp3Details mp3, String start_time, String end_time) {
        int[] data = GetDataByTime.getDataByTime(mp3.getPath(), mp3.getStart_time(), start_time, end_time);
        Mp3Data retData = new Mp3Data();
        retData.setIsNow(0);
        retData.setChannel0(data.toString());
        String jsonStr = JSON.toJSONString(retData);
        if (showDiagramWebSocket.serviceOn())    // websocket发送
            showDiagramWebSocket.sendMessage(jsonStr);
        return JSON.toJSONString(retData);
    }

    /**
     *  存储录制音频的部分
     *
     */
    public void insertMusicList(RecordDetails rd) {
        System.out.println(rd);
        this.mp3DataDao.insertMusicList(rd);
    }

    public List<RecordDetails> loadMusicList(String userId) {
        Long userIdLong = Long.parseLong(userId);
        return this.mp3DataDao.loadMusicList(userIdLong);

        /*String retStr = "{";
        for (int i = 0; i < music_list.size(); i++) {
            String str = JSON.toJSONString(music_list.get(i));
            retStr = retStr + str;
            if (i != music_list.size() - 1)
                retStr = retStr + ",";
        }
        retStr = retStr + "}";*/
    }

    @Override
    public void playMusicByRecordId(String recordId, HttpServletResponse response) {
        Long id = Long.parseLong(recordId);
        System.out.println("Record:"+id);
        RecordDetails record = mp3DataDao.loadRecordDetails(id);
        System.out.println(record);

        // 首先在music_list表之中查询有没有录制该记录，然后music_details之中查询音乐
        if (record == null) throw new NullPointerException("没有该录制记录！");

        String start_time = record.getStart_time();
        String end_time = record.getEnd_time();
        Mp3Details mp3 = loadMp3Details(record.getUserId(), start_time, end_time);
        System.out.println(mp3);
        if (mp3 == null) throw new NullPointerException("没有找到对应的时间段内的音频！");
        String path = mp3.getPath();

        sendMp3Utils.SetResponse(path, response);
        send_playback(mp3, start_time, end_time);
    }
}
